/*
 *所有关于tb_project_reviewspecial类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*tb_project_reviewspecialService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using MYData.Core.BaseProvider;
using MYData.Core.Extensions.AutofacManager;
using MYData.Entity.DomainModels;
using System.Linq;
using MYData.Core.Utilities;
using System.Linq.Expressions;
using MYData.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using MYData.System.IRepositories;
using System.Threading.Tasks;
using MYData.Core.EFDbContext;
using System.Collections.Generic;
using MYData.Core.Enums;
using Mapster;
using MYData.System.IServices;
using System.Xml.Linq;
using System;
using MYData.Entity.DomainModels.ApiEntity.Dto;

namespace MYData.System.Services
{
    public partial class tb_project_reviewspecialService : Itb_project_reviewspecialService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Itb_project_reviewspecialRepository _repository;//访问数据库
        private readonly VOLContext db;

        [ActivatorUtilitiesConstructor]
        public tb_project_reviewspecialService(
            Itb_project_reviewspecialRepository dbRepository,
            IHttpContextAccessor httpContextAccessor,
            VOLContext db
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            this.db = db;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        WebResponseContent webResponse = new WebResponseContent();


        /// <summary>
        /// 专家列表显示+查询+分页
        /// 专家名称模糊查询,专家组下拉查询
        /// </summary>
        /// <returns></returns>
        //异步方法

     

        public object GetlistReviewspecialAsync(string? specialistName = "", string? panelID = "")

        {
            //获取表中数据判断删除状态
            var list = db.Set<tb_project_reviewspecial>().AsQueryable().Where(x=>x.isDel==0);
            if (!string.IsNullOrEmpty(specialistName))
            {
                list = list.Where(x => x.specialistName.Contains(specialistName));
            }
            if (!string.IsNullOrEmpty(panelID))
            {
                list = list.Where(x => x.panelID.Contains(panelID));
            }
            
            //结果集作为匿名对象返回
            //list = list.Skip((index - 1) * size).Take(size);
            //return list;

            var info = list.ToList();
            foreach (var item in info)
            {
                //循环获取专家组id调用
                item.panelID = getSplit(item.panelID); 
            }
            //如果提供了specialistName参数，则在专家名称包含所提供的字符串的地方对列表进行筛选

            //结果集作为匿名对象返回
            //info = info.Skip((index - 1) * size).Take(size).ToList();
            return info;
        }

       
        public string getSplit(string id)
        {
            //进行逗号分割
            string[] ids = id.Split(',');
            List<string> list=new List<string>();
            foreach (var item in ids)
            {
                //根据主表外键id查询外键名称
                var name = db.Set<tb_project_reviewspecial_group>().Where(x => x.panelID == item).FirstOrDefault().panelName.ToString();
                list.Add(name);
            }
            //list转字符串
            return String.Join(",", list);

        }

        /// <summary>
        /// 专家列表添加
        /// </summary>
        /// <param name="inputdto"></param>
        /// <returns></returns>
        public WebResponseContent InsertReviewspecialAsync(tb_project_reviewspecialinput_Dto inputdto)
        {
            //开始事务
            db.Database.BeginTransaction();

            try
            {
                //将dto转换为实体
                var model = inputdto.Adapt<tb_project_reviewspecial>();

                // 添加基本信息到数据库
                db.Set<tb_project_reviewspecial>().Add(model);
                if (db.SaveChanges() < 1)
                {
                    //主表添加失败  事务回滚
                    db.Database.RollbackTransaction();
                    return webResponse.Error("专家添加失败！！！");
                }
                db.Database.CommitTransaction();
                return webResponse.OK("添加成功", true);
            }
            catch (global::System.Exception)
            {
                //回滚
                db.Database.RollbackTransaction();
                return webResponse.Error("专家添加失败!!!");

            }

        }


        /// <summary>
        /// 专家列表修改
        /// </summary>
        /// <param name="outputdto"></param>
        /// <returns></returns>
        public WebResponseContent updateReviewspecialAsync(tb_project_reviewspecialioutput_Dto outputdto)
        {
            //开始事务
            db.Database.BeginTransaction();

            try
            {
                //将dto转换为实体
                var model = outputdto.Adapt<tb_project_reviewspecial>();

                // 添加基本信息到数据库
                db.Set<tb_project_reviewspecial>().Update(model);
                if (db.SaveChanges() < 1)
                {
                    //主表添加失败  事务回滚
                    db.Database.RollbackTransaction();
                    return webResponse.Error("专家修改失败！！！");
                }
                db.Database.CommitTransaction();
                return webResponse.OK("修改成功", true);
            }
            catch (global::System.Exception)
            {
                //回滚
                db.Database.RollbackTransaction();
                return webResponse.Error("专家修改失败!!!");

            }

        }

        /// <summary>
        ///伪删除
        /// </summary>
        /// <param name="id">申报Id</param>
        /// <returns></returns>
        public WebResponseContent deleteReviewspecialAsync(string id)
        {

            db.Database.BeginTransaction();
            try
            {
                //根据附件表id获取数据
                var delreviewspecial = db.Set<tb_project_reviewspecial>().Find(id);
                //修改状态
                delreviewspecial.isDel = 1;
                //专家表状态修改
                db.Update(delreviewspecial);

                if (db.SaveChanges() < 1)
                {
                    //失败 事务回滚
                    db.Database.RollbackTransaction();
                    return webResponse.Error("删除失败！！！");
                }

                //返回状态
                db.SaveChanges();
                //提交事务
                db.Database.CommitTransaction();
                //使用检索到的信息返回一个成功的响应
                return webResponse.OK("删除成功");
            }
            catch (global::System.Exception)
            {
                //回滚
                db.Database.RollbackTransaction();
                return webResponse.Error("删除失败！！！");
            }

        }

        /// <summary>
        /// 专家组查询唯一
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<WebResponseContent> BaseReviewspecialspecialistName(string specialistName)
        {
            var  list = (await _repository.FindAsync(x => x.specialistName.Equals(specialistName)));
            bool result = true;
            if (list.Count() > 0)
                result = false;
            return webResponse.OK("专家名称查重", result);
        }



        /// <summary>
        /// 根据id查询数据
        /// </summary>
        /// <param name="specialistID"></param>
        /// <returns></returns>
        public WebResponseContent reviewspecialID(string specialistID)
        {

            var list = db.Set<tb_project_reviewspecial>().Where(s => s.specialistID.Equals(specialistID));
                       
                      

            return webResponse.OK("根据id获取成功", list);

        }

    }
}
